package com.bite.redisDemo;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Slf4j
public class RedisDemoList {
    public static void test1(Jedis jedis) {
        System.out.println("lpush 和 lrange");
        jedis.flushAll();

        // 头插法
        jedis.lpush("key1","111","222","333");

        List<String> result1 = jedis.lrange("key1", 0, -1);
        System.out.println(result1);

    }

    private static void test2(Jedis jedis) {
        System.out.println("rpush");
        jedis.flushAll();

        // 头插法
        jedis.rpush("key1","111","222","333");

        List<String> result1 = jedis.lrange("key1", 0, -1);
        System.out.println(result1);

    }

    public static void test3(Jedis jedis){
        System.out.println("lpop");
        jedis.flushAll();
        jedis.lpush("key1","111","222","333");
        List<String> result1 = jedis.lrange("key1", 0, -1);
        System.out.println(result1);

        String lpopResult1 = jedis.lpop("key1");
        System.out.println("lpopResult1: "+lpopResult1);

        String lpopResult2 = jedis.lpop("key1");
        System.out.println("lpopResult2: "+lpopResult2);

        String lpopResult3 = jedis.lpop("key1");
        System.out.println("lpopResult3: "+lpopResult3);

        String lpopResult4 = jedis.lpop("key1");
        System.out.println("lpopResult4: "+lpopResult4);
    }

    public static void test4(Jedis jedis){
        System.out.println("rpop");
        jedis.flushAll();
        jedis.lpush("key1","111","222","333");
        List<String> result1 = jedis.lrange("key1", 0, -1);
        System.out.println(result1);

        String rpopResult1 = jedis.rpop("key1");
        System.out.println("rpopResult1: "+rpopResult1);

        String rpopResult2 = jedis.rpop("key1");
        System.out.println("rpopResult2: "+rpopResult2);

        String rpopResult3 = jedis.rpop("key1");
        System.out.println("rpopResult3: "+rpopResult3);

        String rpopResult4 = jedis.rpop("key1");
        System.out.println("rpopResult4: "+rpopResult4);
    }

    public static void test5(Jedis jedis){
        System.out.println("blpop");
        jedis.flushAll();

        // 返回结果是一个二元组,一个是从哪个 key 对应的list中删除的,一个是删除的list是什么
        List<String> result = jedis.blpop(300, "key");
        System.out.println("result.get(0): "+result.get(0));
        System.out.println("result.get(1): "+result.get(1));
    }

    public static void test6(Jedis jedis){
        System.out.println("llen");
        jedis.flushAll();

        jedis.lpush("key","111","222","333");
        long llenResult = jedis.llen("key");
        System.out.println("len:"+llenResult);
    }



    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
            test6(jedis);


        }catch (Exception e){
            log.error("内部错误: "+e.getMessage());
        }
    }


}
